package com.wodwl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wodwl.dao.BbsDao;
import com.wodwl.dao.BbsDao.Mode;
import com.wodwl.dao.PostDao;
import com.wodwl.dao.UserDao;
import com.wodwl.entity.Bbs;
import com.wodwl.entity.Post;
import com.wodwl.entity.User;

@Controller
public class BbsController {
	private String bbs_add_view="/bbs/bbs_add";
	private String bbs_add_success_view="/bbs/bbs_list";
	private String bbs_list_view="/bbs/bbs_list";
	private String bbs_show_view="/bbs/bbs_show";
	private String bbs_editor_view="/bbs/bbs_editor";
	private String post_view="/bbs/post";
	
	@RequestMapping("/bbs/post_view.do")
	 public ModelAndView replyBbsView(HttpServletRequest request,HttpServletResponse response) {
		Map map=new Hashtable();
		BbsDao dao2=new BbsDao();
		Bbs bbs=dao2.getBbsByBid(Integer.parseInt(request.getParameter("bid")));
		map.put("bbs", bbs);
		PostDao daor=new PostDao();
		//List<Post> postList=daor.getAllPostsByBid(bbs.getBid());
		List<Post> postList=new ArrayList<Post>();
		map.put("postList", postList);
		String to_user=request.getParameter("to_user");
		map.put("to_user", to_user);
		return new ModelAndView(getPost_view(),map);
		
	}
	@RequestMapping("/bbs/post.do")
	 public ModelAndView replyBbs(HttpServletRequest request,HttpServletResponse response) {
		
		Post reply=new Post();
		reply.setContent(request.getParameter("content"));
		//reply.setBid(Integer.parseInt(request.getParameter("bid")));
		reply.setFrom_user(request.getParameter("from_user"));
		reply.setTo_user(request.getParameter("to_user"));
		reply.setStatus(1);
		PostDao daor=new PostDao();
		daor.update(reply, com.wodwl.dao.PostDao.Mode.INSERT);
		

		Map map=new Hashtable();
		BbsDao dao2=new BbsDao();
		Bbs bbs=dao2.getBbsByBid(Integer.parseInt(request.getParameter("bid")));
		map.put("bbs", bbs);
		
		//List<Post> postList=daor.getAllPostsByBid(bbs.getBid());
				List<Post> postList=new ArrayList<Post>();
		map.put("postList", postList);
		return new ModelAndView(getBbs_show_view(),map);
		
	}

	@RequestMapping("/bbs/bbs_show.do")
	 public ModelAndView showBbs(HttpServletRequest request,HttpServletResponse response) {
		Map map=new Hashtable();
		BbsDao dao2=new BbsDao();
		Bbs bbs=dao2.getBbsByBid(Integer.parseInt(request.getParameter("bid")));
		map.put("bbs", bbs);
		PostDao daor=new PostDao();
		//List<Post> postList=daor.getAllPostsByBid(bbs.getBid());
				List<Post> postList=new ArrayList<Post>();
		map.put("postList", postList);

		return new ModelAndView(getBbs_show_view(),map);
		
	}
	
	@RequestMapping("/bbs/bbs_editor1.do")
	 public ModelAndView editorInfo1(HttpServletRequest request,HttpServletResponse response) {

		Map map=new Hashtable();
		BbsDao dao2=new BbsDao();
		Bbs bbs=dao2.getBbsByBid(Integer.parseInt(request.getParameter("bid")));
		map.put("bbs", bbs);
		return new ModelAndView(getBbs_editor_view(),map);
	}
	
	
	@RequestMapping("/bbs/bbs_editor2.do")
	 public ModelAndView editorInfo2(HttpServletRequest request,HttpServletResponse response) {
		
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String username=request.getParameter("username");
		String category=request.getParameter("category");
		
		
		Bbs bbs=new Bbs();
		bbs.setBid(Integer.parseInt(request.getParameter("bid")));
		bbs.setTitle(title);
		bbs.setContent(content);
		bbs.setUsername(username);
		bbs.setCategory(category);
		bbs.setCreate_time(System.currentTimeMillis());
		
		BbsDao dao=new BbsDao();
		dao.update(bbs, Mode.UPDATE);
		Map map=new Hashtable();
		map.put("bbs", bbs);
		
		PostDao daor=new PostDao();
		//List<Post> postList=daor.getAllPostsByBid(bbs.getBid());
				List<Post> postList=new ArrayList<Post>();
		map.put("postList", postList);
		
		return new ModelAndView(getBbs_show_view(),map);
		
	}
	
	
	

	@RequestMapping("/bbs/bbs_add.do")
    public ModelAndView addBbs(HttpServletRequest request,HttpServletResponse response) {
		
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String username=request.getParameter("username");
		String category=request.getParameter("category");
		
		
		Bbs bbs=new Bbs();
		bbs.setTitle(title);
		bbs.setContent(content);
		bbs.setUsername(username);
		bbs.setCategory(category);
		
		BbsDao dao=new BbsDao();
		dao.update(bbs, Mode.INSERT);
		
		dao=new BbsDao();
		List<Bbs> bbss=dao.getAllBbss();
		for(int i=0;i<bbss.size();i++){
			bbs=bbss.get(i);
			if(bbs.getContent()!=null&&bbs.getContent().length()>150)
				bbs.setContent(bbs.getContent().substring(0,151));
		}
		Map map=new Hashtable();
		map.put("bbss",bbss);
		return new ModelAndView(getBbs_list_view(),map);
    }
	

	@RequestMapping("/bbs/bbs_list.do")
    public void listBbs(HttpServletRequest request,HttpServletResponse response) throws IOException, JSONException {

		JSONArray jsonArray=new JSONArray();
		BbsDao dao=new BbsDao();
		List<Bbs> bbss=dao.getAllBbss();
		for(int i=0;i<bbss.size();i++){
			Bbs bbs=bbss.get(i);
			if(bbs.getContent()!=null&&bbs.getContent().length()>150)
				bbs.setContent(bbs.getContent().substring(0,151));
			jsonArray.put(bbs.toJsonObject());
		}

		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(jsonArray);
    }
	
	@RequestMapping("/bbs/bbs_add_view.do")
    public String addBbsView(HttpServletRequest request,HttpServletResponse response) {
        return getBbs_add_view();
    }
	
	
	public String getBbs_add_view() {
		return bbs_add_view;
	}

	public String getBbs_add_success_view() {
		return bbs_add_success_view;
	}

	public String getBbs_list_view() {
		return bbs_list_view;
	}

	public String getBbs_show_view() {
		return bbs_show_view;
	}


	public String getBbs_editor_view() {
		return bbs_editor_view;
	}

	public String getPost_view() {
		return post_view;
	}
	

	
	
}
